#include "stdafx.h"
#include "ui_widthbox.h"
#include <qpixmap.h>
#include "pf_debug.h"

/**
 * Default Constructor. You must call init manually if you choose
 * to use this constructor.
 */
UI_WidthBox::UI_WidthBox(QWidget* parent, const char* name)
        : QComboBox(parent, name) {

    showByLayer = false;
    showUnchanged = false;
    unchanged = false;
}

/**
 * Constructor that calls init and provides a fully functional 
 * combobox for choosing widths.
 *
 * @param showByLayer true: Show attributes ByLayer, ByBlock
 */
UI_WidthBox::UI_WidthBox(bool showByLayer, bool showUnchanged,
                         QWidget* parent, const char* name)
        : QComboBox(parent, name) {
    init(showByLayer, showUnchanged);
}


/**
 * Destructor
 */
UI_WidthBox::~UI_WidthBox() {}


/**
 * Initialisation (called from constructor or manually but only
 * once).
 *
 * @param showByLayer true: Show attributes ByLayer, ByBlock
 */
void UI_WidthBox::init(bool showByLayer, bool showUnchanged) {
    this->showByLayer = showByLayer;
	this->showUnchanged = showUnchanged;

    if (showUnchanged) {
        insertItem(QPixmap(":ui/width00.png"), tr("- Unchanged -"));
    }
    if (showByLayer) {
        insertItem(QPixmap(":ui/width00.png"), tr("By Layer"));
        insertItem(QPixmap(":ui/width00.png"), tr("By Block"));
    }
    insertItem(QPixmap(":ui/width01.png"), tr("Default"));
    insertItem(QPixmap(":ui/width01.png"), tr("0.00mm"));
    insertItem(QPixmap(":ui/width01.png"), tr("0.05mm"));
    insertItem(QPixmap(":ui/width01.png"), tr("0.09mm"));
    insertItem(QPixmap(":ui/width01.png"), tr("0.13mm (ISO)"));
    insertItem(QPixmap(":ui/width01.png"), tr("0.15mm"));
    insertItem(QPixmap(":ui/width01.png"), tr("0.18mm (ISO)"));
    insertItem(QPixmap(":ui/width01.png"), tr("0.20mm"));
    insertItem(QPixmap(":ui/width01.png"), tr("0.25mm (ISO)"));
    insertItem(QPixmap(":ui/width01.png"), tr("0.30mm"));
    insertItem(QPixmap(":ui/width03.png"), tr("0.35mm (ISO)"));
    insertItem(QPixmap(":ui/width03.png"), tr("0.40mm"));
    insertItem(QPixmap(":ui/width04.png"), tr("0.50mm (ISO)"));
    insertItem(QPixmap(":ui/width05.png"), tr("0.53mm"));
    insertItem(QPixmap(":ui/width05.png"), tr("0.60mm"));
    insertItem(QPixmap(":ui/width06.png"), tr("0.70mm (ISO)"));
    insertItem(QPixmap(":ui/width07.png"), tr("0.80mm"));
    insertItem(QPixmap(":ui/width08.png"), tr("0.90mm"));
    insertItem(QPixmap(":ui/width09.png"), tr("1.00mm (ISO)"));
    insertItem(QPixmap(":ui/width10.png"), tr("1.06mm"));
    insertItem(QPixmap(":ui/width10.png"), tr("1.20mm"));
    insertItem(QPixmap(":ui/width12.png"), tr("1.40mm (ISO)"));
    insertItem(QPixmap(":ui/width12.png"), tr("1.58mm"));
    insertItem(QPixmap(":ui/width12.png"), tr("2.00mm (ISO)"));
    insertItem(QPixmap(":ui/width12.png"), tr("2.11mm"));

    connect(this, SIGNAL(activated(int)),
            this, SLOT(slotWidthChanged(int)));

    setCurrentItem(0);
    slotWidthChanged(currentItem());
}

/**
 * Sets the currently selected width item to the given width.
 */
void UI_WidthBox::setWidth(LX_Define::LineWidth w) {

    PF_DEBUG->print("UI_WidthBox::setWidth %d\n", (int)w);

    int offset = (int)showByLayer*2 + (int)showUnchanged;

    switch (w) {
    case LX_Define::WidthByLayer:
        if (showByLayer) {
            setCurrentItem(0 + (int)showUnchanged);
        } else {
        	PF_DEBUG->print(PF_Debug::D_WARNING,
            	"UI_WidthBox::setWidth: Unsupported width.");
        }
        break;
    case LX_Define::WidthByBlock:
        if (showByLayer) {
            setCurrentItem(1 + (int)showUnchanged);
        } else {
        	PF_DEBUG->print(PF_Debug::D_WARNING,
            	"UI_WidthBox::setWidth: Unsupported width.");
        }
        break;
    case LX_Define::WidthDefault:
        setCurrentItem(0 + offset);
        break;
    case LX_Define::Width00:
        setCurrentItem(1 + offset);
        break;
    case LX_Define::Width01:
        setCurrentItem(2 + offset);
        break;
    case LX_Define::Width02:
        setCurrentItem(3 + offset);
        break;
    case LX_Define::Width03:
        setCurrentItem(4 + offset);
        break;
    case LX_Define::Width04:
        setCurrentItem(5 + offset);
        break;
    case LX_Define::Width05:
        setCurrentItem(6 + offset);
        break;
    case LX_Define::Width06:
        setCurrentItem(7 + offset);
        break;
    case LX_Define::Width07:
        setCurrentItem(8 + offset);
        break;
    case LX_Define::Width08:
        setCurrentItem(9 + offset);
        break;
    case LX_Define::Width09:
        setCurrentItem(10 + offset);
        break;
    case LX_Define::Width10:
        setCurrentItem(11 + offset);
        break;
    case LX_Define::Width11:
        setCurrentItem(12 + offset);
        break;
    case LX_Define::Width12:
        setCurrentItem(13 + offset);
        break;
    case LX_Define::Width13:
        setCurrentItem(14 + offset);
        break;
    case LX_Define::Width14:
        setCurrentItem(15 + offset);
        break;
    case LX_Define::Width15:
        setCurrentItem(16 + offset);
        break;
    case LX_Define::Width16:
        setCurrentItem(17 + offset);
        break;
    case LX_Define::Width17:
        setCurrentItem(18 + offset);
        break;
    case LX_Define::Width18:
        setCurrentItem(19 + offset);
        break;
    case LX_Define::Width19:
        setCurrentItem(20 + offset);
        break;
    case LX_Define::Width20:
        setCurrentItem(21 + offset);
        break;
    case LX_Define::Width21:
        setCurrentItem(22 + offset);
        break;
    case LX_Define::Width22:
        setCurrentItem(23 + offset);
        break;
    case LX_Define::Width23:
        setCurrentItem(24 + offset);
        break;
    default:
        break;
    }

    slotWidthChanged(currentItem());
}



/**
 * Sets the pixmap showing the width of the "By Layer" item.
 */
void UI_WidthBox::setLayerWidth(LX_Define::LineWidth w) {
    if (showByLayer) {
        QPixmap pixmap;
        switch(w) {
        default:
        case LX_Define::Width00:
            pixmap = QPixmap(":ui/width00.png");
            break;
        case LX_Define::Width01:
        case LX_Define::Width02:
            pixmap = QPixmap(":ui/width01.png");
            break;
        case LX_Define::Width03:
        case LX_Define::Width04:
            pixmap = QPixmap(":ui/width02.png");
            break;
        case LX_Define::Width05:
        case LX_Define::Width06:
            pixmap = QPixmap(":ui/width03.png");
            break;
        case LX_Define::Width07:
        case LX_Define::Width08:
            pixmap = QPixmap(":ui/width04.png");
            break;
        case LX_Define::Width09:
        case LX_Define::Width10:
            pixmap = QPixmap(":ui/width05.png");
            break;
        case LX_Define::Width11:
        case LX_Define::Width12:
            pixmap = QPixmap(":ui/width06.png");
            break;
        case LX_Define::Width13:
        case LX_Define::Width14:
            pixmap = QPixmap(":ui/width07.png");
            break;
        case LX_Define::Width15:
        case LX_Define::Width16:
            pixmap = QPixmap(":ui/width08.png");
            break;
        case LX_Define::Width17:
        case LX_Define::Width18:
            pixmap = QPixmap(":ui/width09.png");
            break;
        case LX_Define::Width19:
        case LX_Define::Width20:
            pixmap = QPixmap(":ui/width10.png");
            break;
        case LX_Define::Width21:
        case LX_Define::Width22:
            pixmap = QPixmap(":ui/width11.png");
            break;
        case LX_Define::Width23:
            //case LX_Define::Width24:
            pixmap = QPixmap(":ui/width12.png");
            break;
        }

        changeItem(pixmap, tr("By Layer"), 0);

        // needed for the first time a layer is added:
        slotWidthChanged(currentItem());
    }
}

/**
 * Called when the width has changed. This method 
 * sets the current width to the value chosen or even
 * offers a dialog to the user that allows him/ her to
 * choose an individual width.
 */
void UI_WidthBox::slotWidthChanged(int index) {

    PF_DEBUG->print("UI_WidthBox::slotWidthChanged %d\n", index);

    bool done = false;

    if (showUnchanged && index==0) {
        unchanged = true;
        done = true;
    } else {
        unchanged = false;
    }

    if (!done && showByLayer) {
        if (index==0 + (int)showUnchanged) {
            currentWidth = LX_Define::WidthByLayer;
            done = true;
        } else if (index==1 + (int)showUnchanged) {
            currentWidth = LX_Define::WidthByBlock;
            done = true;
        }
    }

    if (!done) {
        switch (index-((int)showByLayer*2)-((int)showUnchanged)) {
        case 0:
            currentWidth = LX_Define::WidthDefault;
            break;
        case 1:
            currentWidth = LX_Define::Width00;
            break;
        case 2:
            currentWidth = LX_Define::Width01;
            break;
        case 3:
            currentWidth = LX_Define::Width02;
            break;
        case 4:
            currentWidth = LX_Define::Width03;
            break;
        case 5:
            currentWidth = LX_Define::Width04;
            break;
        case 6:
            currentWidth = LX_Define::Width05;
            break;
        case 7:
            currentWidth = LX_Define::Width06;
            break;
        case 8:
            currentWidth = LX_Define::Width07;
            break;
        case 9:
            currentWidth = LX_Define::Width08;
            break;
        case 10:
            currentWidth = LX_Define::Width09;
            break;
        case 11:
            currentWidth = LX_Define::Width10;
            break;
        case 12:
            currentWidth = LX_Define::Width11;
            break;
        case 13:
            currentWidth = LX_Define::Width12;
            break;
        case 14:
            currentWidth = LX_Define::Width13;
            break;
        case 15:
            currentWidth = LX_Define::Width14;
            break;
        case 16:
            currentWidth = LX_Define::Width15;
            break;
        case 17:
            currentWidth = LX_Define::Width16;
            break;
        case 18:
            currentWidth = LX_Define::Width17;
            break;
        case 19:
            currentWidth = LX_Define::Width18;
            break;
        case 20:
            currentWidth = LX_Define::Width19;
            break;
        case 21:
            currentWidth = LX_Define::Width20;
            break;
        case 22:
            currentWidth = LX_Define::Width21;
            break;
        case 23:
            currentWidth = LX_Define::Width22;
            break;
        case 24:
            currentWidth = LX_Define::Width23;
            break;
        default:
            break;
        }
    }
    //currentWidth = (LX_Define::LineWidth)(index-1);
    //currentWidth = (LX_Define::LineWidth)(currentText().left(4).toDouble()*100);
    //}

    PF_DEBUG->print("Current width is (%d): %d\n",
                    index, ((int)currentWidth));

    emit widthChanged(currentWidth);
}

// EOF
